package com.erp.web.erp.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.erp.base.business.entity.AppUser;
import com.erp.base.business.entity.ExpertUser;
import com.erp.base.business.entity.MemberConsultAnswer;
import com.erp.base.business.entity.MemberConsultQuestion;
import com.erp.base.common.utils.OperationResult;
import com.erp.web.erp.service.ErpUserService;

@RequestMapping("/erp/user")
@RestController
public class ErpUserController{
	@Autowired
	private ErpUserService erpUserService;
	
		
	/**
	 * 登录接口
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	public OperationResult login(@RequestBody AppUser appUser,HttpServletRequest request){
		return erpUserService.login(appUser.getUsername(),appUser.getPassword(),request);
	}
	
	/**
	 * 登出接口
	 * @return
	 */
/*	@RequestMapping("/loginOut")
	public ModelAndView  loginOut(HttpServletRequest request){
		AppUser appUser=(AppUser)request.getSession().getAttribute("appUser");
		if(appUser!=null){
			request.getSession().removeAttribute("appUser");
		}
		return new ModelAndView("/erp/index/index");
	}*/
	@RequestMapping("/loginOut")
	public OperationResult  loginOut(HttpServletRequest request){
		AppUser appUser=(AppUser)request.getSession().getAttribute("appUser");
		if(appUser!=null){
			request.getSession().removeAttribute("appUser");
		}
		return OperationResult.buildSuccessResult("退出成功！");
	}
	
	/**
	 * 注册接口
	 * @param appUser
	 * @return
	 */
	@PostMapping("/register")
	public OperationResult register(@RequestBody AppUser appUser,HttpServletRequest request){
		return erpUserService.register(appUser,request);
	}
	
	/**
	 * 获取用户自己的问题列表
	 * @param isAnswered
	 * @param request
	 * @return
	 */
	@RequestMapping("/memberSelfQuestions")
	public OperationResult getMemberSelfQuestions(@RequestParam("isAnswer")String isAnswer,HttpServletRequest request){
		return erpUserService.getMemberSelfQuestions(isAnswer,request);
	}
	
	/**
	 * 根据专家id获取专家回答列表
	 * @param isAnswer 0：未回答 1 回答
	 * @param expertId
	 * @return
	 */
	@RequestMapping("/memberExpectQuestions")
	public OperationResult getMemberExpertQuestions(@RequestParam("isAnswer")String isAnswer,@RequestParam("userId") Long userId,HttpServletRequest request){
		return erpUserService.getMemberExpertQuestions(isAnswer,userId,request);
	}
	/**
	 * 问题详情页接口
	 * @param memberQuestionId 问题ID
	 * @return
	 */
	@RequestMapping("/detailtAnswers")
	public OperationResult getDetailtAnswers(@RequestParam("memberQuestionId") Long memberQuestionId){
		return erpUserService.getDetailtAnswers(memberQuestionId);
	}
	/**
	 * 专家/会员 回复接口
	 * @param memberConsultAnswer
	 * @param request
	 * @return
	 */
	@PostMapping("/memberAnswers")
	public OperationResult savememberAnswers(@RequestBody MemberConsultAnswer memberConsultAnswer,HttpServletRequest request){
		return erpUserService.savememberAnswers(memberConsultAnswer,request);
	}
	
	/**
	 * 获取服务类型（申请专家服务页面）
	 * @return
	 */
	@RequestMapping("/getServerType")
	public OperationResult getServerType(){
		return erpUserService.getServerType();
	}
	
	/**
	 *申请专家服务接口 
	 * @param expertUser
	 * @param request
	 * @return
	 */
	@PostMapping("/applyExpert")
	public OperationResult applyExpert(@RequestBody ExpertUser expertUser,HttpServletRequest request){
		return erpUserService.applyExpert(expertUser,request);
	}
	
	/**
	 * 查看专家申请状态/进度
	 * @param userId
	 * @return
	 */
	@RequestMapping("/getApplyStatus")
	public OperationResult getApplyStatus(Long userId){
		return erpUserService.getApplyStatus(userId);
	}
	/**
	 * 编辑个人资料
	 * @param appUser
	 * @param file
	 * @param request
	 * @return
	 */
	@PostMapping("/editAppUser")
	public OperationResult editAppUser(@RequestBody AppUser appUser,HttpServletRequest request){
		return erpUserService.editAppUser(appUser,request,appUser.getFile());
	}
	/**
	 * 会员提出问题
	 * @param memberConsultQuestion
	 * @param request
	 * @return
	 */
	@PostMapping("/askQuestions")
	public OperationResult askQuestions(@RequestBody MemberConsultQuestion memberConsultQuestion,HttpServletRequest request){
		return erpUserService.askQuestions(memberConsultQuestion,request);
	}
}
